<?php
/**
 *  WB_PDOObjStore class unit tests
 *
 *  @author Jean-Lou Dupont
 *  @version @@package-version@@
 */

require_once 'PHPUnit/Framework.php';
require_once 'web-bloks/CacheableObjectClass/CacheableObjectClass.php';
require_once 'web-bloks/Identifier/Identifier.php';
require_once 'web-bloks/PDOObjStore/PDOObjStore.php';

class UnitTest extends PHPUnit_Framework_TestCase
{
	var $db = null;
	var $obj = null;
	
    protected function setUp()
    {
    	/*
	 	 * 		'key'	=> 'object key name', // [optional] for BaseClass 
	 	 * 		'dsn'	=> 'mysql:host=localhost;dbname=test',
	 	 * 		'user'	=> 'user',
	 	 * 		'pass'	=> 'password',
	 	 * 		'table'	=> 'table_where_objects_are_stored'
    	 */
		$params = array(
			'dsn'	=> 'mysql:host=localhost;dbname=test',
			'user'	=> 'test',
			'pass'	=> 'test',
			'table' => 'objects',
		);
		
		$this->db = new WB_PDOObjStore( $params );
		$this->obj = new WB_CacheableObjectClass();
    }
 
 	protected function tearDown()
	{
		unset( $this->db );
		unset( $this->obj );
	}
 
    public function testSimple()
    {
        $this->assertEquals(true, is_object( $this->db ) );
    }
	/**
	 * Schema Check
	 * 
	 * @return 
	 */
	public function testSchema() //#1
	{
		$result = true;
		try
		{
			$this->db->init();
			$this->db->checkSchema();
		}
		catch( Exception $e )
		{
			$result = false;
		}
        $this->assertEquals(true, $result );		
	}

	public function testCreate0()
	{
		$result = true;
		$code = null;
		
		$id = new WB_Identifier();
		$id->set( 'test/testobj1' );
		
		$this->obj->someVariable = "test";

		try
		{
			$this->db->init();
			
			$this->obj->setModified();
			$this->obj->setExpire();
			$this->obj->setDisposable();
			
			$code = $this->db->create( $id, $this->obj );


		}
		catch( Exception $e )
		{
			$result = false;
		}
        $this->assertEquals(true, $result );		
		
	}
	#DELETE the object we just created
	public function testDelete0()
	{
		$result = true;
		
		$id = new WB_Identifier();
		$id->set( 'test/testobj1' );
		
		try
		{
			$this->db->init();
			
			$code = $this->db->delete( $id );
		}
		catch( Exception $e )
		{
			$result = false;
		}
        $this->assertEquals(true, $result );		
        $this->assertEquals(true, $code );				
	}	

// ^^^

	public function testCreate2()
	{
		$result = true;
		$code = null;
		
		$id = new WB_Identifier();
		$id->set( 'test/testobj2' );
		
		$this->obj->someVariable = "test";

		try
		{
			$this->db->init();
			
			$this->obj->setModified();
			$this->obj->setExpire();
			$this->obj->setDisposable();
			
			$code = $this->db->create( $id, $this->obj );


		}
		catch( Exception $e )
		{
			$result = false;
		}
        $this->assertEquals(true, $result );		
        $this->assertEquals(true, $code );				
		
	}

	public function testSave1() //#2
	{
		$result = true;
		$code = null;
		
		$id = new WB_Identifier();
		$id->set( 'test/testobj2' );
		
		$this->obj->someVariable = "test";
		$this->obj->someOtherVariable = "testOther";		
		
		try
		{
			$this->db->init();
			
			$this->obj->setModified();
			$this->obj->setExpire();
			$this->obj->setDisposable();
			
			$code = $this->db->save( $id, $this->obj );
		}
		catch( Exception $e )
		{
			$result = false;
		}
        $this->assertEquals(true, $result );		
        $this->assertEquals(true, $code );		
	}

# ^^^^^

	public function testRestore1() //#3
	{
		$result = true;
		
		$id = new WB_Identifier();
		$id->set( 'test/testobj2' );
		
		try
		{
			$this->db->init();
			
			$obj = $this->db->restore( $id );
		}
		catch( Exception $e )
		{
			$result = false;
		}
        $this->assertEquals(true, $result );
        $this->assertEquals(true, is_object( $obj ) );	
		$this->assertEquals(true, $obj instanceof WB_CacheableObjectClass );
		$this->assertEquals( "test", $obj->someVariable );
		//var_dump( $obj );	
	}

	/**
	 * Tries to restore an otherwise not existent object
	 * @return 
	 */
	public function testRestore2() //#4
	{
		$result = true;
		
		$id = new WB_Identifier();
		$id->set( 'test/inexistant' );
		
		try
		{
			$this->db->init();
			
			$obj = $this->db->restore( $id );
		}
		catch( Exception $e )
		{
			$result = false;
		}
        $this->assertEquals(false, $result );		
	}


	public function testCreateDuplicate()
	{
		$result = true;
		$code = null;
		
		$id = new WB_Identifier();
		$id->set( 'test/testobj2' );
		
		$this->obj->someVariable = "test";

		try
		{
			$this->db->init();
			
			$this->obj->setModified();
			$this->obj->setExpire();
			$this->obj->setDisposable();
			
			$code = $this->db->create( $id, $this->obj );
		}
		catch( Exception $e )
		{
			$result = false;
		}
        $this->assertEquals(true, $result );		
        $this->assertEquals(false, $code );				
		
	}
	

	

	#DELETION TEST
	
	public function testDelete1() //#5
	{
		$result = true;
		
		$id = new WB_Identifier();
		$id->set( 'test/testobj2' );
		
		try
		{
			$this->db->init();
			
			$code = $this->db->delete( $id );
		}
		catch( Exception $e )
		{
			$result = false;
		}
        $this->assertEquals(true, $result );		
        $this->assertEquals(true, $code );				
	}	

	
	#DELETION test2: non-existant object
	 
	public function testDelete2() //#6
	{
		$result = true;
		
		$id = new WB_Identifier();
		$id->set( 'test/nonexistant' );
		
		try
		{
			$this->db->init();
			
			$code = $this->db->delete( $id );
		}
		catch( Exception $e )
		{
			$result = false;
		}
        $this->assertEquals(true, $result );		
        $this->assertEquals(false, $code );				
	}	

}